package com.lcw.library.imagepicker.view.addimgview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.lcw.library.imagepicker.ImagePicker;
import com.lcw.library.imagepicker.R;
import com.lcw.library.imagepicker.data.MediaFile;
import com.lcw.library.imagepicker.imagelist.ImageListUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author hdl
 */
public class AddImageRecyclerView extends RecyclerView {

    private Activity mActivity;
    private Fragment mFragment;
    private int requestCode;
    private AddImageAdapter addImageAdapter;
    private List<MediaFile> mMediaFileList = new ArrayList<>();
    private int layoutRes;
    private int addImgResId;
    private int delImgResId;
    private int itemDecorationLeftRight;
    private int itemDecorationTopBottom;

    private boolean showCamera = true;//是否显示拍照Item，默认显示
    private boolean showImage = true;//是否显示图片，默认显示
    private boolean showVideo = false;//是否显示视频，默认不显示
    private boolean filterGif = true;//是否过滤GIF图片，默认过滤
    private int maxCount = 1;//最大选择数量，默认为1
    private boolean singleType = true;//是否只支持选单类型（图片或者视频）

    public AddImageRecyclerView(Context context) {
        this(context, null);
    }

    public AddImageRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddImageRecyclerView(final Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AddImageRecyclerView, 0, 0);
        if (ta != null) {
            addImgResId = ta.getResourceId(R.styleable.AddImageRecyclerView_add_img_src, R.drawable.release_btn_uploadphotos);
            itemDecorationLeftRight = ta.getDimensionPixelOffset(R.styleable.AddImageRecyclerView_item_decoration_left_right, 0);
            itemDecorationTopBottom = ta.getDimensionPixelOffset(R.styleable.AddImageRecyclerView_item_decoration_top_bottom, 0);
            layoutRes = ta.getResourceId(R.styleable.AddImageRecyclerView_item_layout_res, R.layout.grid_item_select_image);
            delImgResId = ta.getResourceId(R.styleable.AddImageRecyclerView_del_img_src, R.drawable.release_icon_delete);
            ta.recycle();
        }
        addImageAdapter = new AddImageAdapter(context, mMediaFileList, addImgResId, maxCount, layoutRes, delImgResId);
        setAdapter(addImageAdapter);
        setNestedScrollingEnabled(false);
        addImageAdapter.setOnImageChangeListener(new AddImageAdapter.OnImageChangeListener() {
            @Override
            public void onAddBtnClick(int position, boolean isAdd) {
                if (isAdd) {
                    ImagePicker.getInstance().showCamera(showCamera)//设置是否显示拍照按钮
                            .showImage(showImage)//设置是否展示图片
                            .showVideo(showVideo)//设置是否展示视频
                            .filterGif(filterGif)//是否过滤GIF图片
                            .setMaxCount(maxCount)//设置最大选择图片数目(默认为1，单选)
                            .setSingleType(singleType)//设置图片视频不能同时选择
                            .setImagePaths(getmMediaFileStrings());//设置历史选择记录
                    if (mActivity != null) {
                        ImagePicker.getInstance().start(mActivity, requestCode);
                    } else if (mFragment != null) {
                        ImagePicker.getInstance().start(mFragment, requestCode);
                    }
                } else {
                    ImageListUtil.showBigImagesStr(context, getmMediaFileStrings(), position);
                }
            }

            @Override
            public void onDelete(MediaFile item) {

            }

        });

        addItemDecoration(new ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(itemDecorationLeftRight, itemDecorationTopBottom, itemDecorationLeftRight, itemDecorationTopBottom);
            }
        });
    }

    public AddImageRecyclerView init(Activity activity, int requestCode, LayoutManager layoutManager) {
        mActivity = activity;
        init(requestCode, layoutManager);
        return this;
    }

    public AddImageRecyclerView init(Fragment fragment, int requestCode, LayoutManager layoutManager) {
        mFragment = fragment;
        init(requestCode, layoutManager);
        return this;
    }

    private void init(int requestCode, LayoutManager layoutManager) {
        this.requestCode = requestCode;
        setLayoutManager(layoutManager);
    }

    /**
     * description: 选完图片onActivityResult需要调用该方法 author: Daniel
     */
    public void onImageActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == this.requestCode) {
                List<String> mImagePaths = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
                mMediaFileList.clear();
                for (String mImagePath : mImagePaths) {
                    mMediaFileList.add(new MediaFile(mImagePath));
                }
                addImageAdapter.sortListData();
                addImageAdapter.notifyDataSetChanged();
            }
        }
    }

    public List<MediaFile> getmMediaFileList() {
        return mMediaFileList;
    }

    public ArrayList<String> getmMediaFileStrings() {
        ArrayList<String> list = new ArrayList<>();
        for (MediaFile mediaFile : mMediaFileList) {
            if (!mediaFile.isAdd()) {
                list.add(mediaFile.getPath());
            }
        }
        return list;
    }

    /**
     * 是否支持相机
     */
    public AddImageRecyclerView showCamera(boolean showCamera) {
        this.showCamera = showCamera;
        return this;
    }

    /**
     * 是否展示图片
     */
    public AddImageRecyclerView showImage(boolean showImage) {
        this.showImage = showImage;
        return this;
    }

    /**
     * 是否展示视频
     */
    public AddImageRecyclerView showVideo(boolean showVideo) {
        this.showVideo = showVideo;
        return this;
    }

    /**
     * 是否过滤GIF图片(默认过滤)
     */
    public AddImageRecyclerView filterGif(boolean filterGif) {
        this.filterGif = filterGif;
        return this;
    }

    /**
     * 图片最大选择数
     */
    public AddImageRecyclerView setMaxCount(int maxCount) {
        this.maxCount = maxCount;
        addImageAdapter.setMaxCount(maxCount);
        return this;
    }

    /**
     * 设置是否单类型选择（只能选图片或者视频）
     */
    public AddImageRecyclerView setSingleType(boolean isSingleType) {
        this.singleType = isSingleType;
        return this;
    }

}
